package com.company.huisuo.biz.dialog

import android.app.Activity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.company.huisuo.R
import com.company.huisuo.biz.adapter.ArtistGiftAdapter
import com.company.huisuo.biz.adapter.ArtistGiftTitleAdapter
import com.company.huisuo.biz.bean.GiftBean
import com.company.huisuo.repository.GiftRepository
import com.lxj.xpopup.core.BottomPopupView
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * 打赏艺人-礼物dialog
 */
class ArtistGiftDialog(
    activity: Activity, private val onSendClick: (GiftBean?) -> Unit
) : BottomPopupView(activity) {

    private val mTitleAdapter by lazy {
        ArtistGiftTitleAdapter()
    }
    private val mGiftAdapter by lazy {
        ArtistGiftAdapter()
    }
    
    private val giftRepository by lazy {
        GiftRepository()
    }

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_artist_gift
    }

    override fun onCreate() {
        super.onCreate()
        findViewById<RecyclerView>(R.id.rcv_gift).apply {
            layoutManager = GridLayoutManager(activity, 8)
            adapter = mGiftAdapter
        }
        mGiftAdapter.addOnItemChildClickListener(R.id.tv_send) { adapter, view, pos ->
            val selectedGift = mGiftAdapter.getSelectedGift()
            onSendClick.invoke(selectedGift)
        }

        initData()
    }

    private fun initData() {
        val titleList = arrayListOf(0, 1, 2, 3, 4)
        mTitleAdapter.submitList(titleList)

        // 使用GiftRepository获取真实的礼物数据
        CoroutineScope(Dispatchers.Main).launch {
            try {
                val result = withContext(Dispatchers.IO) {
                    giftRepository.getAppGiftList()
                }
                result.onSuccess { giftList ->
                    mGiftAdapter.submitList(giftList)
                }.onFailure { error ->
                    // 如果获取失败，使用默认数据
                    loadDefaultGifts()
                }
            } catch (e: Exception) {
                // 异常情况下使用默认数据
                loadDefaultGifts()
            }
        }
    }
    
    private fun loadDefaultGifts() {
        // 默认礼物数据，作为网络请求失败时的备用
        val defaultGifts = listOf(
            com.company.huisuo.biz.bean.GiftBean(
                id = 1,
                giftCode = "gift_1",
                giftName = "礼物1",
                giftAmount = 99.0,
                giftProjectionMaterial = "",
                giftLogo = "ic_gift_1"
            ),
            com.company.huisuo.biz.bean.GiftBean(
                id = 2,
                giftCode = "gift_2",
                giftName = "礼物2",
                giftAmount = 199.0,
                giftProjectionMaterial = "",
                giftLogo = "ic_gift_2"
            ),
            com.company.huisuo.biz.bean.GiftBean(
                id = 3,
                giftCode = "gift_3",
                giftName = "礼物3",
                giftAmount = 299.0,
                giftProjectionMaterial = "",
                giftLogo = "ic_gift_3"
            ),
            com.company.huisuo.biz.bean.GiftBean(
                id = 4,
                giftCode = "gift_4",
                giftName = "礼物4",
                giftAmount = 399.0,
                giftProjectionMaterial = "",
                giftLogo = "ic_gift_4"
            )
        )
        mGiftAdapter.submitList(defaultGifts)
    }
}